<?php if(!defined('HDPHP_PATH'))exit;C('SHOW_NOTICE',FALSE);?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <script src="http://127.0.0.1/mogujie/Static/js/jquery-1.8.2.min.js" type="text/javascript" charset="utf-8"></script>
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/mogujie/Static/hdjs/hdjs.css"/>
    <script type="text/javascript" src="http://127.0.0.1/mogujie/Static/ueditor1_4_3/ueditor.config.js"></script>
    <script type="text/javascript" src="http://127.0.0.1/mogujie/Static/ueditor1_4_3/ueditor.all.js"></script>
    <script type="text/javascript" charset="utf-8" src="http://127.0.0.1/mogujie/Static/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
    <title>修改商品</title>
    <style type="text/css">
		.checkbox{
			 margin-left:5px;
			 width:90px;
			 margin-top: 10px;
		}
		textarea{
			width: 300px;
			height: 100px;
		}
    </style>
</head>
<body>
<div class="wrap">
    <div class="hd-title-header">编辑商品</div>
    <form action="" method="post" class="form-inline hd-form" id="shopForm">
    		<script type="text/javascript">
    			//异步获取商品属性和规格
    				function getSpecAttr(cid){
    					//通过cid找到相对应的类型属性
    					$.ajax({
    						type:"post",
    						url:"<?php echo U('Shop/getTypeAttr');?>",
    						data:{cid : cid},
    						dataType:'json',
    						success:function(phpData){
    							//定义规格和属性字符串
    							var attr = '';
    							var spec = '';
    							//循环json组合字符串
    							$.each(phpData, function(k,v) {
    								//如果是属性的时候
    								  if(v.class == 0){
    								  		 attr += '<tr style="color:red">' + 
						            			'<th class="w100">'+v.name+'</th>' + 
						            			'<th>' + 
							            			'<select name="attr['+v.taid+']">' + 
							            				'<option value="">------请选择------</option>';
							            				var option = '';
							            				$.each(v.type_value, function(kk,vv) {    
							            					 option += '<option value="'+vv+'">'+vv+'</option>';                                                       
							            				});
							            			attr += option;
							            			attr += '</select>' + 
						            			'</th>' + 
						            			'<th></th>' + 
						            		'</tr>';  
    								  }else{
    								  	//如果是规格的时候
    								  	spec += '<tr style="color: blue;">' + 
							            		'<th class="w100">'+v.name+'</th>' + 
							            		'<th width="50">' + 
							            			'<select name="spec['+v.taid+'][value][]">' + 
							            				'<option value="">请选择</option>';
							            				var option = '';
							            				$.each(v.type_value, function(kk,vv) {    
							            					 option += '<option value="'+vv+'">'+vv+'</option>';                                                       
							            				});
							            				//连接option
							            				spec += option;
								            			spec += '</select>' + 
											            		'</th>' + 
											            		'<th>附加价格' +
											            			'<input type="text" name="spec['+v.taid+'][price][]" id="" value=""/>' + 
											            			' <a href="javascript:;" class="hd-btn hd-btn-primary add-spec">添加规格</a>' + 
											            		'</th>' + 
											            '</tr>';
    								  }
    								                                                      
    							});
    							//插入到属性的表格中
    							$('#attr').html(attr);
    							$('#spec').html(spec);
    						}
    					});
    				}
    				
    				
    				
    			$(function(){
    				//旧的cid
    				var oldCid = <?php echo $oldData['cid'];?>;
    				var oldAttr ='<?php echo $selected['attr'];?>';

    				var oldSpec = '<?php echo $selected['spec'];?>';
    				$('#attr').html(oldAttr);
    				$('#spec').html(oldSpec);
    				$('select[name=cid]').change(function(){
    					//获得选择分类的cid
    					var cid = $(this).val();
    					if(cid == oldCid){
    						//插入到属性的表格中
    						$('#attr').html(oldAttr);
    						$('#spec').html(oldSpec);
    					}else{
    						//调用函数，函数在上面
    						getSpecAttr(cid);
    					}
    					
    				})
    				//点击添加规格，规格会多一个
    				$('.add-spec').live('click',function(){
    					var tr = $(this).parents('tr');
    					//克隆一个父级tr
    					var cloneTr = tr.clone();
    					cloneTr.find('a').html('删除规格').removeClass('hd-btn-primary').removeClass('add-spec').addClass('hd-btn-danger remove-spec');
    					//插入到当前tr的下一行
    					tr.after(cloneTr);
    				})
    				//点击删除规格
    				$('.remove-spec').live('click',function(){
    					$(this).parents('tr').remove();
    				})
    				
    				//表单提交，把规格和属性放到隐藏域
    				$('#shopForm').submit(function(){
    					//抓到规格和属性的内容
    					var attr = $('#attr').html();
    					var spec = $('#spec').html();
    					//存到隐藏域，为了提交给php
    					var hidden = "<input type='hidden' name='selected[attr]' value='"+attr+"'/>";
    					hidden += "<input type='hidden' name='selected[spec]' value='"+spec+"'/>";
    					//在后面追加
    					$(this).append(hidden);
    					
    				})
    				
    				//让选中规格和属性都有selected属性
    				$('select').live('change',function(){
    					//让当前表单选中的option加一个selected,让其他的兄弟元素清除selected
    					$(this).find('option:selected').attr('selected',true).siblings().removeAttr('selected');
    				})
    				//让添加价格存入缓存
	    			$('input').live('blur',function(){
	    				$(this).attr('value',$(this).val());
	    			})
    				
    				//点击x 删除列表图片
    		$('.close').click(function(){
    			//父级td
    			var box = $(this).parents('p');
    			//获得完整路径
    			var path = $(this).attr('path');
    			//获得对应的gid
    			var gid = $(this).attr('gid');
    			$.ajax({
    				type:"post",
    				url:"<?php echo U('delImg');?>",
    				data:{path : path,gid : gid},
    				success:function(){
    					box.remove();
    				}
    			});
    		})
			
			//点击x 删除图册图片
    		$('.delPic').click(function(){
    			//父级td
    			var box = $(this).parents('p');
    			//获得完整路径
    			var path = $(this).attr('path');
    			$.ajax({
    				type:"post",
    				url:"<?php echo U('delPic');?>",
    				data:{path : path},
    				success:function(){
    					box.remove();
    				}
    			});
    		})
    				
    			})
    		</script>
        <table class="hd-table hd-table-list">
        		<tr>
                <th class="w100">所属分类</th>
                <td>
                    <select name="cid" class="w200">
                    	<option value="0">------请选择------</option>
                    	<?php foreach ($cateData as $k=>$v){?>
                    		<option value="<?php echo $v['cid'];?>"     <?php if($oldData['cid'] == $v['cid']){ ?>selected<?php } ?> ><?php echo $v['_name'];?></option>
                    	<?php }?>
                    </select>
                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">所属品牌</th>
                <td>
                    <select name="bid" class="w200">
                    	<option value="0">----请选择----</option>
                    		<?php foreach ($brandData as $k=>$v){?>
                    			<option value="<?php echo $v['bid'];?>"     <?php if($oldData['bid'] == $v['bid']){ ?>selected<?php } ?>><?php echo $v['bname'];?></option>
                    		<?php }?>
                    </select>
                </td>
                <td></td>
            </tr>
           
             <tr>
                <th class="w100">商品名称</th>
                <td class="w100">
                    <input type="text" name="gname" class="w200" value="<?php echo $oldData['gname'];?>"/> 
                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">单位</th>
                <td class="w100">
                    <input type="text" name="unit" class="w200" value="<?php echo $oldData['unit'];?>" /> 
                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">市场价</th>
                <td class="w100">
                    <input type="text" name="marketprice" class="w200" value="<?php echo $oldData['marketprice'];?>"/> 

                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">商城价</th>
                <td class="w100">
                    <input type="text" name="shopprice" class="w200" value="<?php echo $oldData['shopprice'];?>" /> 

                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">点击次数</th>
                <td class="w100">
                    <input type="text" name="click" class="w200" value="<?php echo $oldData['click'];?>" /> 
					
                </td>
                <td></td>
            </tr>
          
            <tr>
                <th class="w100">商品属性</th>
                <td></td>
                <td></td>
            </tr>
            </table>
            <!--商品属性的字符串要插入到这个表格中-->
            <table id="attr" class="hd-table hd-table-list">
            	
            </table>
            <table class="hd-table hd-table-list">
            <tr>
                <th class="w100" width="310">商品规格</th>
                <td></td>
                <td></td>
            </tr>
           </table>
           <!--把循环好的规格放到这个表格中-->
           <table id="spec" class="hd-table hd-table-list">
           	
           </table>
			<script type="text/javascript" src="http://127.0.0.1/mogujie/Static/uploadify/jquery.uploadify.min.js"></script>
			<link href="http://127.0.0.1/mogujie/Static/uploadify/uploadify.css" type="text/css" rel="stylesheet"></link>
           <table class="hd-table hd-table-list">
            <tr>
                <th class="w100">列表图</th>
                <td>
                	    <?php if($oldData['pic']){ ?>
	                	<p>
	                		<img src="http://127.0.0.1/mogujie/<?php echo $oldData['pic'];?>" style="width: 100px;"/>
	                		<a href='javascript:;' style='color: red;' class='del-img close' path='<?php echo $oldData['pic'];?>'>X</a>
	                		<input type="hidden" name="pic" value="<?php echo $oldData['pic'];?>" />
	                	</p>
                	<?php } ?>
					 <div lab="uploadFile">
	            			<input id="listpic" type="file" multiple="true">
	            			<span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
	            			<script type="text/javascript">
	                		$(function() {
	                    		$('#listpic').uploadify({
	                        		'formData': {//POST数据
	                            '<?php echo session_name();?>' : '<?php echo session_id();?>',
	                        },
	                        'fileTypeDesc' : '上传文件',//上传描述
	                        'fileTypeExts' : '*.jpg;*.png',
	                        'swf'      : 'http://127.0.0.1/mogujie/Static/uploadify/uploadify.swf',
	                        'uploader' : '<?php echo U("uploadList");?>',//上传操作方法
	                        'buttonText':'选择文件',
	                        'fileSizeLimit' : '2000KB',
	                        'uploadLimit' : 1,//上传文件数
	                        'width':65,
	                        'height':25,
	                        'successTimeout':10,//等待服务器响应时间
	                        'removeTimeout' : 0.2,//成功显示时间
	                        'onUploadSuccess' : function(file, data, response) {
	                            data=$.parseJSON(data);
		                            var imageUrl = data.image?data.url:'http://127.0.0.1/mogujie/Static/Admin/image/default.png';
		                            var li="<li path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"' class='hd-w80 hd-h70'/><a href='javascript:;' style='color: red;' class='del-img'>X</a><input type='hidden' name='pic' value='"+data.path+"'/></li>";
		                            $("#uploadList ul").prepend(li);
	                        }
	                    });
	                });
	            </script>
	            <div id="uploadList">
	                <ul>
	                </ul>
	            </div>
	        </div>
                </td>
                <td></td>
            </tr>
             <tr>
                <th class="w100">商品图册</th>
                <td>
                	    <?php if($detailData['shop_pic']){ ?>
                	<?php foreach ($detailData['shop_pic'] as $k=>$v){?>
                		<p>
                			<img src="http://127.0.0.1/mogujie/<?php echo $v;?>" style="width: 100px;"/>
                			<a href='javascript:;' style='color: red;' class='del-imgs delPic' path='<?php echo $v;?>'>X</a>
                			<input type="hidden" name="shopPic[]" value='<?php echo $v;?>'/>
                		</p>
                	<?php }?>
                	 <?php } ?>
                    <div lab="uploadFile">
	            			<input id="pic" type="file" multiple="true">
	            			<span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
	            			<script type="text/javascript">
	                		$(function() {
	                    		$('#pic').uploadify({
	                        		'formData': {//POST数据
	                            '<?php echo session_name();?>' : '<?php echo session_id();?>',
	                        },
	                        'fileTypeDesc' : '上传文件',//上传描述
	                        'fileTypeExts' : '*.jpg;*.png',
	                        'swf'      : 'http://127.0.0.1/mogujie/Static/uploadify/uploadify.swf',
	                        'uploader' : '<?php echo U("uploadPic");?>',//上传操作方法
	                        'buttonText':'选择文件',
	                        'fileSizeLimit' : '2000KB',
	                        'uploadLimit' : 1000,//上传文件数
	                        'width':65,
	                        'height':25,
	                        'successTimeout':10,//等待服务器响应时间
	                        'removeTimeout' : 0.2,//成功显示时间
	                        'onUploadSuccess' : function(file, data, response) {
	                           data=$.parseJSON(data);
	                            var imageUrl = data.image?data.url:'http://127.0.0.1/mogujie/Static/Admin/image/default.png';
	                            var li="<li path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"' class='hd-w80 hd-h70'/><a href='javascript:;' style='color: red;' class='del-img'>X</a><input type='hidden' name='shopPic[]' value='"+data.path+"'/></li>";
	                            $("#picList ul").prepend(li);
	                        }
	                    });
	                });
	            </script>
	            <div id="picList">
	                <ul>
	 
	                </ul>
	            </div>
	        </div> 
                </td>
                <td></td>
            </tr>
           
             <tr>
                <th class="w100">商品详细</th>
                <td>
                   <script id="editor" type="text/plain" style="width:500px;height:400px;"name='intro'><?php echo $detailData['intro'];?></script>
                    <script type="text/javascript">
       					 var ue = UE.getEditor('editor');
   					</script>
                </td>
                <td></td>
            </tr>
        
          <tr>
                <th class="w100">售后服务</th>
                <td>
                   <script id="editor1" type="text/plain" style="width:500px;height:400px;"name='service'><?php echo $detailData['service'];?></script>
                    <script type="text/javascript">
       					 var ue = UE.getEditor('editor1');
   					</script>
                </td>
                <td></td>
            </tr>
        
        </table>
        <div class="position-bottom">
            <input type="submit" class="hd-btn hd-btn-success" value="修改"/>
        </div>
    </form>
</div>
</body>
</html>